//
//  ZCXActivityVC.m
//  zctx
//
//  Created by apple on 16/4/18.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCXActivityVC.h"
#import "YYaoViewController.h" //摇一摇
#import "ZCNewTabBarController.h"//小卫士活动
#import "ZCNoanswerVC.h"
#import "DZWebViewController.h"
#import "ZCActivityModelContent.h"

@interface ZCXActivityVC ()

@property (nonatomic,strong)UIImageView *imgView1;
@property (nonatomic,strong)UIButton *btnView2;
@property (nonatomic,strong)UIButton *btnView3;
@property (nonatomic,strong)UIButton *btnView4;
@property (nonatomic,strong)UIButton *btnView5;
@property (nonatomic,strong)UIButton *btnView6;
@property (nonatomic,strong)UIImageView *imgView7;
@property (nonatomic,strong)UIImageView *imgView8;
@property (nonatomic,strong)UIImageView *imgView9;

@property (nonatomic,strong)NSMutableArray *cityArray;
@property (nonatomic,strong)NSString *isexam;

@end

@implementation ZCXActivityVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    if (self.isCity ==2) {
        [self requestMemberInfo];
    }
    
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.cityArray removeAllObjects];
    self.isCity = 1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initnavegation];
//    if ([[UIDevice currentDevice].systemVersion floatValue] >7.0 ) {
////        self.extendedLayoutIncludesOpaqueBars = NO;
//        self.edgesForExtendedLayout = UIRectEdgeNone;
//    }
    self.navigationItem.title = @"食品安全校园行";
    
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 216 +135*4 +195 +133 +167);

    self.scrollView.bounces = NO;
    [self.scrollView addSubview:self.imgView1];
    self.imgView1.frame = CGRectMake(0, 0, SCREEN_WIDTH, 216);
    
    [self.scrollView addSubview:self.btnView2];
    self.btnView2.frame = CGRectMake(0, 216, SCREEN_WIDTH, 135);
    
    [self.scrollView addSubview:self.btnView3];
    self.btnView3.frame = CGRectMake(0,216 +135, SCREEN_WIDTH, 135);
    
    [self.scrollView addSubview:self.btnView4];
    self.btnView4.frame = CGRectMake(0, 216 +135*2, SCREEN_WIDTH/2, 135);
    
    [self.scrollView addSubview:self.btnView5];
    self.btnView5.frame = CGRectMake(SCREEN_WIDTH/2, 216 +135*2, SCREEN_WIDTH/2, 135);
    
    [self.scrollView addSubview:self.btnView6];
    self.btnView6.frame = CGRectMake(0, 216 +135*3, SCREEN_WIDTH, 135);
    
    [self.scrollView addSubview:self.imgView7];
    self.imgView7.frame = CGRectMake(0, 216 +135*4, SCREEN_WIDTH, 195);
    
    [self.scrollView addSubview:self.imgView8];
    self.imgView8.frame = CGRectMake(0, 216 +135*4 +195, SCREEN_WIDTH, 133);
    
    [self.scrollView addSubview:self.imgView9];
    self.imgView9.frame = CGRectMake(0, 216 +135*4 +195 +133, SCREEN_WIDTH, 167);
    
    
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark获取用户活动资料
-(void)requestMemberInfo{

    __weak ZCXActivityVC *weakSelf = self;
    [NetworkManager requestAcshiyaojuGetMemberInfomid:UserModel.mid password:UserModel.password success:^(NSDictionary *dictResponse) {
        if ([[dictResponse objectForKey:@"content"] isKindOfClass:[NSDictionary class]]){
            NSDictionary *dict = (NSDictionary *)[dictResponse objectForKey:@"content"];
            
//            NSString *region_code = [NSString stringWithFormat:@"%@",[dict objectForKey:@"region_code"]];
            weakSelf.isexam = [NSString stringWithFormat:@"%@",[dict objectForKey:@"is_exam"]];
            
            if ([weakSelf.isexam isEqualToString:@"0"]) {
                [_btnView4 setBackgroundImage:[UIImage imageNamed:@"activity4_no"] forState:UIControlStateNormal];
                [_btnView5 setBackgroundImage:[UIImage imageNamed:@"activity5_no"] forState:UIControlStateNormal];
                [self requestGetCities];
            }else{
                [_btnView4 setBackgroundImage:[UIImage imageNamed:@"activity4_yes"] forState:UIControlStateNormal];
                [_btnView5 setBackgroundImage:[UIImage imageNamed:@"activity5_yes"] forState:UIControlStateNormal];
            }
        }
    } failure:^(NSError *error) {
        
    }];

}

#pragma mark 获取城市
-(void)requestGetCities{
    
    [NetworkManager requestAcshiyaojuGetCitiesmid:UserModel.mid password:UserModel.password success:^(NSDictionary *dictResponse) {
        
        if ([[dictResponse objectForKey:@"content"] isKindOfClass:[NSArray class]]){
            NSArray *array = (NSArray *)[dictResponse objectForKey:@"content"];
            
            for (NSDictionary *dict in array) {
                ZCActivityModelContent *model = [ZCActivityModelContent modelObjectWithDictionary:dict];
                [self.cityArray addObject:model];
            }
            [self SelectCtiy];
        }
    } failure:^(NSError *error) {
        
    }];
}

-(void)SelectCtiy{
    CGFloat xWidth = self.view.bounds.size.width - 60.0f;
    CGFloat yHeight = 44 + self.cityArray.count *44;
    CGFloat yOffset = (self.view.bounds.size.height - yHeight)/2.0f;
    ZCityPopoverListView *poplistview = [[ZCityPopoverListView alloc] initWithFrame:CGRectMake(10, yOffset, xWidth, yHeight)];
    poplistview.delegate = self;
    poplistview.datasource = self;
    poplistview.listView.scrollEnabled = FALSE;
    [poplistview setTitle:@"请选择活动城市"];
    [poplistview show];
}

#pragma mark 学习
-(void)clickStudyBtn{
    
    NSString *url = [NSString stringWithFormat:@"%@/%@%@",[NetworkManager strURLBasic],@"index.php/M/Acshiyaoju/exam_learn_app.html?schoolid=1&mid=",UserModel.mid];
    [self gotoWebViewController:url andTitle:@"食品安全知识学习"];
}

#pragma mark 考试
-(void)clickExamBtn{
    
    NSString *url = [NSString stringWithFormat:@"%@/%@%@",[NetworkManager strURLBasic],@"index.php/M/Acshiyaoju/exam_enter_for_app.html?mid=",UserModel.mid];
    [self gotoWebViewController:url andTitle:@"食品安全知识考试"];
}

#pragma mark 摇一摇 抽奖
-(void)clickLuckdrawBtn{
    if ([self.isexam isEqualToString:@"0"]) {
        [[[iToast makeText:@"还没参加食品安全考试"]setGravity:iToastGravityCenter] show];
    }else{
        YYaoViewController *vc = [[YYaoViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark 小卫士 参赛
-(void)clickCompetitionBtn{
    
    if ([self.isexam isEqualToString:@"0"]) {
        [[[iToast makeText:@"还没参加食品安全考试"]setGravity:iToastGravityCenter] show];
    }else{
        ZCNewTabBarController *newvc = [[ZCNewTabBarController alloc]init];
        newvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:newvc animated:YES];
    }
}

#pragma mark 你投票 我检测 投票
-(void)clickVoteBtn{
    
    NSString *url = [NSString stringWithFormat:@"%@/%@%@",[NetworkManager strURLBasic],@"index.php/M/Acshiyaoju/like_product_list_app.html?mid=",UserModel.mid];
    [self gotoWebViewController:url andTitle:@"你投票我检测"];
}

#pragma mark 未参加食品安全小卫士答题{
-(void)judgeNoanswer:(NSString *)type{
    ZCNoanswerVC *answer = [[ZCNoanswerVC alloc]init];
    answer.hidesBottomBarWhenPushed = YES;
    answer.type = type;
    [self.navigationController pushViewController:answer animated:YES];
}

#pragma mark 公用WebView方法
-(void)gotoWebViewController:(NSString *)url andTitle:(NSString *)title{
    NSLog(@"url = %@",url);
    
    DZWebViewController *vc = [[DZWebViewController alloc] init];
    vc.strURL = url;
    vc.title = title;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];

}
#pragma mark - UIPopoverListViewDataSource

- (UITableViewCell *)popoverListView:(ZCityPopoverListView *)popoverListView
                    cellForIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                    reuseIdentifier:identifier];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    ZCActivityModelContent *model = self.cityArray[indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}

- (NSInteger)popoverListView:(ZCityPopoverListView *)popoverListView
       numberOfRowsInSection:(NSInteger)section
{
    return self.cityArray.count;
}

#pragma mark - UIPopoverListViewDelegate
- (void)popoverListView:(ZCityPopoverListView *)popoverListView
     didSelectIndexPath:(NSIndexPath *)indexPath
{
    ZCActivityModelContent *model = self.cityArray[indexPath.row];
    [self requestUpdateCity:model.regionCode];
}

- (CGFloat)popoverListView:(ZCityPopoverListView *)popoverListView
   heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

-(void)requestUpdateCity:(NSString *)code{
    [NetworkManager requestAcshiyaojuUpdateMemberInfomid:UserModel.mid password:UserModel.password region_code:code success:^(NSDictionary *dictResponse) {
        
//        if (!([dictResponse[@"err"] intValue] == 0))
//        {
//             [[[iToast makeText:dictResponse[@"msg"]]setGravity:iToastGravityCenter] show];
//        }
//        else
//        {
//            [[[iToast makeText:dictResponse[@"msg"]]setGravity:iToastGravityCenter] show];
//        }
    } failure:^(NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(UIImageView *)imgView1{
    if (!_imgView1) {
        _imgView1 = [[UIImageView alloc]initWithFrame:CGRectZero];
        _imgView1.image = [UIImage imageNamed:@"activity1"];
    }
    return _imgView1;
}
-(UIImageView *)imgView7{
    if (!_imgView7) {
        _imgView7 = [[UIImageView alloc]initWithFrame:CGRectZero];
        _imgView7.image = [UIImage imageNamed:@"activity7"];
    }
    return _imgView7;
}

-(UIImageView *)imgView8{
    if (!_imgView8) {
        _imgView8 = [[UIImageView alloc]initWithFrame:CGRectZero];
        _imgView8.image = [UIImage imageNamed:@"activity8"];
    }
    return _imgView8;
}
-(UIImageView *)imgView9{
    if (!_imgView9) {
        _imgView9 = [[UIImageView alloc]initWithFrame:CGRectZero];
        _imgView9.image = [UIImage imageNamed:@"activity9"];
    }
    return _imgView9;
}

-(UIButton *)btnView2{
    if (!_btnView2) {
        _btnView2 = [[UIButton alloc]initWithFrame:CGRectZero];
        [_btnView2 setBackgroundImage:[UIImage imageNamed:@"activity2"] forState:UIControlStateNormal];
        _btnView2.adjustsImageWhenHighlighted = NO;
        [_btnView2 addTarget:self action:@selector(clickStudyBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnView2;
}

-(UIButton *)btnView3{
    if (!_btnView3) {
        _btnView3 = [[UIButton alloc]initWithFrame:CGRectZero];
        [_btnView3 setBackgroundImage:[UIImage imageNamed:@"activity3"] forState:UIControlStateNormal];
        _btnView3.adjustsImageWhenHighlighted = NO;
        [_btnView3 addTarget:self action:@selector(clickExamBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnView3;
}
-(UIButton *)btnView4{
    if (!_btnView4) {
        _btnView4 = [[UIButton alloc]initWithFrame:CGRectZero];
        _btnView4.adjustsImageWhenHighlighted = NO;
        [_btnView4 addTarget:self action:@selector(clickLuckdrawBtn) forControlEvents:UIControlEventTouchUpInside];
        
        [_btnView4 setBackgroundImage:[UIImage imageNamed:@"activity4_yes"] forState:UIControlStateNormal];
    }
    return _btnView4;
}

-(UIButton *)btnView5{
    if (!_btnView5) {
        _btnView5 = [[UIButton alloc]initWithFrame:CGRectZero];
        [_btnView5 setBackgroundImage:[UIImage imageNamed:@"activity5_yes"] forState:UIControlStateNormal];
        _btnView5.adjustsImageWhenHighlighted = NO;
        [_btnView5 addTarget:self action:@selector(clickCompetitionBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnView5;
}

-(UIButton *)btnView6{
    if (!_btnView6) {
        _btnView6 = [[UIButton alloc]initWithFrame:CGRectZero];
        [_btnView6 setBackgroundImage:[UIImage imageNamed:@"activity6"] forState:UIControlStateNormal];
        _btnView6.adjustsImageWhenHighlighted = NO;
        [_btnView6 addTarget:self action:@selector(clickVoteBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnView6;
}

-(NSMutableArray *)cityArray{
    if (!_cityArray) {
        _cityArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _cityArray;
}

@end
